package cn.yunhe.spring.struts.controller;

import cn.yunhe.spring.mybatis.model.Goods;
import cn.yunhe.spring.mybatis.model.Page;
import cn.yunhe.spring.mybatis.service.IGoodsService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/29.
 */
public class GoodsAction extends ActionSupport implements ModelDriven<Object> {
    @Autowired
    private IGoodsService goodsService;
    /**
     * 商品参数
     * */
    private Goods goods;
    /**
     * 分页的每页大小
     * */
    private int pageSize;
    /**
     * 当前页码参数
     * */
    private int pageNum;
    /**
     * 返回当前页的数据对象
     * */
    private Page page;

    public IGoodsService getGoodsService() {
        return goodsService;
    }

    public void setGoodsService(IGoodsService goodsService) {
        this.goodsService = goodsService;
    }

    public Goods getGoods() {
        return goods;
    }

    public void setGoods(Goods goods) {
        this.goods = goods;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    /**
     * 查询商品
     * */
    public String findGoods(){
        goods=goodsService.getGoods(goods);
        ServletActionContext.getRequest().setAttribute("good",goods);
        return SUCCESS;
    }

    /**
     * 按条件查询商品
     * */
    public String queryLikeUsers(){
        Map<String,Object> cond=new HashMap<String,Object>();
        cond.put("pageSize", pageSize);
        cond.put("pageNum", pageNum);
        cond.put("goods", goods);
        page = goodsService.queryGoods(cond);
        return "success";
    }

    /**
     * 删除商品
     * */
    public String delGoods(){
        goodsService.delGoods(goods);
        return SUCCESS;
    }

    /**
     * 编辑商品
     * */
    public String editGoods(){
        goodsService.editGoods(goods);
        return SUCCESS;
    }

    /**
     *添加商品
     * */
    public String addGoods(){
        goodsService.addGoods(goods);
        return SUCCESS;
    }

    /**
     * 重写getModel方法
     * */
    public Object getModel() {
        if(goods==null){
            goods=new Goods();
        }
        return goods;
    }
}
